import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 参数设置
sampling_frequency = 3200  # 采样频率，单位：Hz
signal_duration = 1         # 信号时长，单位：秒

# 生成时间向量
t = np.arange(0, signal_duration, 1/sampling_frequency)
df = pd.read_csv('lvbo06_02_23_50_25.csv', encoding='utf-8')

# 假设我们要读取名为'ColumnName'的列
column_name = 'after'
column_data = df[column_name].tolist()  # 将列数据转换为列表
# 生成电流信号，这里我们使用一个简单的正弦波作为示例
I = df[0:3200]  # 50Hz的基波

# 窗口大小和步长
window_size = 0.02  # 窗口大小，单位：秒
step_size = 0.01     # 步长，单位：秒

# 计算窗口数量
num_windows = int((signal_duration - window_size) / step_size) + 1

# 初始化有效值数组
I_rms_over_time = np.zeros(num_windows)

# 滑动窗口计算有效值
for i in range(num_windows):
    start_index = int(i * step_size * sampling_frequency)
    end_index = int((i * step_size + window_size) * sampling_frequency)
    window = I[start_index:end_index]
    I_rms_over_time[i] = np.sqrt(np.mean(window**2))

# 计算每个窗口的中心时间点
window_times = np.linspace(0, signal_duration - window_size, num=num_windows)

# 绘制原始电流波形和随时间变化的有效值波形
plt.figure(figsize=(12, 6))

plt.subplot(2, 1, 1)
plt.plot(t, I, label='Current Waveform')
plt.title('Current Waveform')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude (A)')
plt.legend()

plt.subplot(2, 1, 2)
plt.plot(window_times, I_rms_over_time, label='RMS over Time', color='red')
plt.title('RMS Value Over Time')
plt.xlabel('Time (s)')
plt.ylabel('RMS Amplitude (A)')
plt.legend()

plt.tight_layout()
plt.show()